#include "blackjack.h"
#include "networking.h"
#include "player.h"
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>


using namespace std;
//in header file
/*#include "cards.h"
#include "deck.h"
#include "player.h"
#include <string>
#define MAX_PLAYER 52
class Blackjack{
public:
  Blackjack();
  Blackjack(int players);
  void Play();
private:
  int numPlayers;
  Player* playerAry[MAX_PLAYER];
};
*/

Blackjack::Blackjack(){
}

Blackjack::Blackjack( int players){
  //cout<<players<<endl;
  numPlayers = players+1;
}

void Blackjack::Play(){
  /* This can be changed later to pass
   * blackjack an array of players
   */
  playerAry[0]= new Player("Dealer",52,9999999);
  int i=1;
  for(i;i<numPlayers;i++){
    cout<<"Player "<<i<<" Name: ";
    string name;
    cin>>name;
    int bank;

    while ((std::cout<<"Player "<<i<<" Bank: ")
	   && !(std::cin >> bank)){
      std::cout << "That's not a number!\n";
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    //    cin>>bank;

    playerAry[i]= new Player(name,52,bank);
    cout<<endl;
  }
  cout<<endl;
  /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   *Change later after more games have been
   *implemented
   */
  
  bool PLAY_AGAIN = true;
  
  while(PLAY_AGAIN){

    //insert code to reset hands
    for(i=0;i<numPlayers;i++)
      playerAry[i]->ResetHand();
    
    Deck newDeck;
    
    /*
      cout<<"printing new deck"<<endl;
      newDeck.PrintDeck();
    */
    
    
    //PLACE BETS!!!!!!!
    //PLACE BETS!!!!!!
    int betAmt, counter;
    cout<<"Placing Bets"<<endl;
    for(i=1;i<numPlayers;i++){
      counter = 0;
      do{
	if((betAmt<=0) && (counter != 0))
	  cout<<"Please enter a positive value"<<endl;
	while((std::cout<<"Player "<<i<<": ")
	      && !(std::cin >> betAmt)) {
	  std::cout << "That's not a number\n";
	  std::cin.clear();
	  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}
	counter++;
      }while(betAmt<=0);
      //cin>>betAmt;
      

      playerAry[i]->PlaceWager(betAmt);
    }

    cout<<endl;
    
    for(i=0;i<numPlayers*2;i++)
      {
	if(i<numPlayers)
	  playerAry[i]->DealCard(newDeck);
	else
	  playerAry[i-numPlayers]->DealCard(newDeck);
	
	//stores the first dealer card which is the face up dealer card in our game
	if(i==0){
	  dealerTopCard = *playerAry[0];
	  playerAry[0]->PrintHand();
	  cout<<endl;
	}
      }
    
    for(i=1;i<numPlayers;i++)
      {
	playerAry[i]->PrintHand();
	//cout<<endl;
      }
   
    
    int tempVal;
    int switchNum;
    for(i=1;i<numPlayers;i++)
      {
	cout<<endl;
	//insert code for betting and hit/stand for each player
	//need code in player class for blackjack value
	//need code to split
	
	//cout<<"Dealer Shows: ";
	dealerTopCard.PrintHand();
	cout<<endl;

	playerAry[i]->PrintHand();
	cout<<endl;
	
	tempVal=playerAry[i]->BlackjackValue();
	cout<<"Player "<<i<<" Hand Value: "<<tempVal<<endl;

	//NEED TO ADD CODE TO SEE IF SPLIT IS AN OPTION!

	bool STAND = false;

	//NEED CODE TO GIVE CORRECT PAYOUT FOR BLACKJACK
	if(tempVal==21)
	  cout<<"BLACKJACK!!!!"<<endl;
	//
	int switchCounter = 0;
	while((tempVal<21)&&!STAND)
	  {
	    if(switchCounter==0){
	      while(
		    (std::cout<<"Would you like to:\n"
		     <<"1)Hit\n2)Stand\n3)Double Down\n")
		    && !(std::cin >>switchNum)){
		std::cout<<"That's not a number\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	      }
	    }
	    else{
	      while(
		    (std::cout<<"Would you like to:\n"
		     <<"1)Hit\n2)Stand\n")
		    && !(std::cin >>switchNum)){
		std::cout<<"That's not a number\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	      }
	    }
	    //cin>>switchNum;
	   
	    if(switchCounter==0){
	      switch(switchNum)
		{
		case 1 : playerAry[i]->DealCard(newDeck);
		  playerAry[i]->PrintHand();
		  tempVal = playerAry[i]->BlackjackValue();
		  cout<<"Player "<<i<<" Hand Value: "<<tempVal<<endl;
		  if(tempVal>21)
		    {
		      cout<<"BUST!\n";
		    }
		  if(tempVal==21)
		    STAND = true;
		  switchCounter++;
		  break;
		  //case 2 stand
		case 2 : STAND = true; 
		  switchCounter++;
		  break;
		  //double down
		case 3 : STAND = playerAry[i]->DoubleDown();
		  if(STAND){
		    playerAry[i]->DealCard(newDeck);
		    playerAry[i]->PrintHand();
		    tempVal = playerAry[i]->BlackjackValue();
		    cout<<"Player "<<i<<" Hand Value: "<<tempVal<<endl;
		    if(tempVal>21)
		      cout<<"BUST!"<<endl;
		  }
		  else
		    cout<<"Not enough money in bank please choose 1 for HIT or 2 for STAND"<<endl;
		  switchCounter++;
		  break;
		default : cout<<"Responce not recognized"<<endl;
		  break;
		}
	    }
	    else{
	      switch(switchNum)
		{
		case 1 : playerAry[i]->DealCard(newDeck);
		  playerAry[i]->PrintHand();
		  tempVal = playerAry[i]->BlackjackValue();
		  cout<<"Player "<<i<<" Hand Value: "<<tempVal<<endl;
		  if(tempVal>21)
		    {
		      cout<<"BUST!\n";
		    }
		  if(tempVal==21)
		    STAND = true;
		  switchCounter++;
		  break;
		  //case 2 stand
		case 2 : STAND = true; 
		  switchCounter++;
		  break;
		default : cout<<"Response not recognized"<<endl;
		  break;
		}
	    }


	  }
      }

    cout<<endl;

    int dealerVal = playerAry[0]->BlackjackValue();
    cout<<"Dealer: "<<dealerVal<<endl;
    while(dealerVal<17)
      {
	playerAry[0]->DealCard(newDeck);
	dealerVal = playerAry[0]->BlackjackValue();
	cout<<"Dealer: "<<dealerVal<<endl;
      }
    dealerVal = playerAry[0]->BlackjackValue();
    if(dealerVal>21)
      cout<<"Dealer BUST!!!"<<endl;
    
    for(i=1;i<numPlayers;i++)
      {
	tempVal=playerAry[i]->BlackjackValue();
	if(((tempVal>dealerVal)&&(tempVal<22)&&(dealerVal<22))||((tempVal<22)&&(dealerVal>21)))
	  {
	    cout<<"Player "<<i<<" Wins!!!"<<endl;
	    //INSERT CODE TO ADJUST BANK ACOUNT FOR PLAYER
	    playerAry[i]->GetWinnings(2*(playerAry[i]->GetWager()));
	  }
	else if((tempVal==dealerVal)&&(tempVal<22)){
	  cout<<"Player "<<i<<" Pushes."<<endl;
	  playerAry[i]->GetWinnings(playerAry[i]->GetWager());
	}
	else
	  {
	    cout<<"Player "<<i<<" Lost."<<endl;
	    playerAry[i]->LostHand();
	  }
      }
    
    cout<<endl;

    cout<<"Would you like to play another round?"<<endl
	<<"Please enter 1 for yes"<<endl
	<<"or 0 for no. ";
    int playAgain;
    cin>>playAgain;
    if(playAgain!=1){
      PLAY_AGAIN = false;
      return;
    }
  }
}
void Blackjack::PlayOnline(){
  Host(true);
  /*
    int hostOrJoin;
  bool checkInput=false;
  bool client;
  do{
    cout<< "Would you like to create a new game (1) or join a game (2)?";
    cin >> hostOrJoin;
    //server
    if (hostOrJoin == 1)
      {
	// run the host function
	// Host();
	client = false;
	checkInput=true;
	Host(client);
      } 
    
    //client
    else if(hostOrJoin==2)
      {
	const char* message;
	char* reply;
	string temp, temp2;
	network gameClient;
	cout<<"Enter ip: ";
	cin>>temp;
	ip = new char[temp.size() + 1];
	copy(temp.begin(), temp.end(),ip);
	ip[temp.size()]='\0';
	temp2="Test_Client";
	message = temp2.c_str();
	reply = gameClient.runClient(ip,message);
	cout<<reply<<endl;
	
	// run the client function
	// Client();	
	client = true;
	checkInput=true;
	Host(client);
      }
    
  }while(!checkInput);
  */
return;
}


void Blackjack::Host(bool client){ // totally NOT FINISHED

  playerAry[0]= new Player("Dealer",52,9999999);
  int i=1;
  for(i;i<2;i++){ // changed to just ask for p1's name
    cout<<"Player "<<i<<" Name: ";
    string name;
    cin>>name;
    int bank;

    while ((std::cout<<"Player "<<i<<" Bank: ")
	   && !(std::cin >> bank)){
      std::cout << "That's not a number!\n";
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    //    cin>>bank;

    playerAry[i]= new Player(name,52,bank);
    cout<<endl;
  }

  //now get player 2's name
  const char* message;
  char* reply;
  network gameServer;
  char* name;
  char* bank;
  message = playerAry[1]->GetName();
  reply = gameServer.runServer(message);
  name = reply;
  message = "gotbank";
  reply = gameServer.runServer(message);
  bank=reply;
  int bankInt = atoi(bank);
  playerAry[2] = new Player(name, 52, bankInt);


  cout<<endl;

  
  bool PLAY_AGAIN = true;
  
  while(PLAY_AGAIN){

    //insert code to reset hands
    for(i=0;i<3;i++)
      playerAry[i]->ResetHand();
    
    Deck newDeck;
    
    /*
      cout<<"printing new deck"<<endl;
      newDeck.PrintDeck();
    */
    
    
    //PLACE BETS!!!!!!!
    //PLACE BETS!!!!!!
    int betAmt, counter;
    cout<<"Placing Bets"<<endl;  //tell client 
    for(i=1;i<2;i++){
      counter = 0;
      do{
	if((betAmt<=0) && (counter != 0))
	  cout<<"Please enter a positive value"<<endl;
	while((std::cout<<"Player "<<i<<": ")
	      && !(std::cin >> betAmt)) {
	  std::cout << "That's not a number\n";
	  std::cin.clear();
	  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}
	counter++;
      }while(betAmt<=0);
      //cin>>betAmt;
      

      playerAry[i]->PlaceWager(betAmt);
    }
   
    stringstream ss;

 
    //ask for client bet
    //tell client what host bet is
    /*
    string bees;
   
    ss << betAmt;
    bees = ss.str();
    
    //cout << bees;
    const char * logitech = bees.c_str();
    // itoa(betAmt, bees, 10);
    //string bees = itoa(betAmt);
    cout << logitech;
    
    reply = gameServer.runServer(logitech);
    int clientBet = atoi(reply);
    playerAry[2]->PlaceWager(clientBet);
    // tell host what client bet is
    */

    //hope this works!!!
const char* message2;
char* reply2;
char* clientBetAmt;
message = "gotbet";
reply2 = gameServer.runServer(message);
clientBetAmt = reply2;
int BetAmtInt = atoi(clientBetAmt);
playerAry[2]->PlaceWager(BetAmtInt);




    cout<<endl;
    
    //deal cards and print hands
    for(i=0;i<6;i++)
      {
	if(i<3)
	  playerAry[i]->DealCard(newDeck);
	else
	  playerAry[i-numPlayers]->DealCard(newDeck);
	
	//stores the first dealer card which is the face up dealer card in our game
	if(i==0){
	  dealerTopCard = *playerAry[0];
	  playerAry[0]->PrintHand();
	  playerAry[0]->PrintHandForClient();
	  cout<<endl;
	}
      }
    
    //for(i=1;i<3;i++)
    //{
    playerAry[1]->PrintHand();
    playerAry[2]->PrintHand();
    cout<<endl;
    playerAry[2]->PrintHandForClient();
    cout<<"finished print hand for client"<<endl;
	//cout<<endl;
	
	//}
   
    
    int tempVal;
    int switchNum;
    for(i=1;i<3;i++)
      {
	cout<<endl;
	//insert code for betting and hit/stand for each player
	//need code in player class for blackjack value
	//need code to split
	
	cout<<"Dealer Shows: ";
	if(i==1)
	  dealerTopCard.PrintHand();
	if(i==2)
	  dealerTopCard.PrintHandForClient();
	cout<<endl;

	if(i==1)
	  playerAry[1]->PrintHand();
	if(i==2)
	  playerAry[2]->PrintHandForClient();
	cout<<endl;
	
	if(i==1){
	tempVal=playerAry[i]->BlackjackValue();
	cout<<"Player "<<i<<" Hand Value: "<<tempVal<<endl;
	//cout<<"DO WE MAKE IT HERE????"<<endl; //take this out later...
	}
	
	else if(i==2){

	  // john you jerk, HEH
	  //cout<<"THIS IS WHERE WE ARE HAVING PROBLEMS!!!"<<endl; 
	  
	  char * thisdoesnothing;
	  //YES WEVE MADE IT TO THIS LINE!!!!
	  thisdoesnothing = gameServer.runServer("Your_Hand_Value_is_");

	  //THIS IS REALLY REALLY WHERE WE ARE HAVING PROBLEMS
	  ss << tempVal;
	  string tempValString = ss.str();
	  //string tempValString = itoa(tempVal);
	  const char* thisdoesnothing2 = tempValString.c_str();
	  thisdoesnothing = gameServer.runServer(thisdoesnothing2);
	  
	  /*
	  //This function is not defined in ANSI-C and is not part of C++, 
	  //but is supported by some compilers.
	  tempVal=playerAry[2]->BlackjackValue();
	  const char* bjValueString;
	  itoa (tempVal,bjValueString,10);
	  char* thisdoesnothing;
	  thisdoesnothing = gameServer.runServer("Your_Hand_Value_Is_");
	  thisdoesnothing = gameServer.runServer(bjValueString);
	  */
	}

	//NEED TO ADD CODE TO SEE IF SPLIT IS AN OPTION!
	//haha no...

	bool STAND = false;

	//NEED CODE TO GIVE CORRECT PAYOUT FOR BLACKJACK
	if((tempVal==21)&&(i=1))
	  cout<<"BLACKJACK!!!!"<<endl;
	else if((tempVal==21)&&(i=2))
	  {
	    char * fiveohthree;
	    fiveohthree = gameServer.runServer("BLACKJACK");
	  }
	//
	if(i=1)
	  {
	    int switchCounter = 0;
	    while((tempVal<21)&&!STAND)
	      {
		if(switchCounter==0){
		  while(
			(std::cout<<"Would you like to:\n"
			 <<"1)Hit\n2)Stand\n3)Double Down\n")
			&& !(std::cin >>switchNum)){
		    std::cout<<"That's not a number\n";
		    std::cin.clear();
		    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		  }
		}
		else{
		  while(
			(std::cout<<"Would you like to:\n"
			 <<"1)Hit\n2)Stand\n")
			&& !(std::cin >>switchNum)){
		    std::cout<<"That's not a number\n";
		    std::cin.clear();
		    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		  }
		}
		//cin>>switchNum;
		
		if(switchCounter==0){
		  switch(switchNum)
		    {
		    case 1 : playerAry[i]->DealCard(newDeck);
		      playerAry[i]->PrintHand();
		      tempVal = playerAry[i]->BlackjackValue();
		      cout<<"Player "<<i<<" Hand Value: "<<tempVal<<endl;
		      if(tempVal>21)
			{
			  cout<<"BUST!\n";
			}
		      if(tempVal==21)
			STAND = true;
		      switchCounter++;
		      break;
		      //case 2 stand
		    case 2 : STAND = true; 
		      switchCounter++;
		      break;
		      //double down
		    case 3 : STAND = playerAry[i]->DoubleDown();
		      if(STAND){
			playerAry[i]->DealCard(newDeck);
			playerAry[i]->PrintHand();
			tempVal = playerAry[i]->BlackjackValue();
			cout<<"Player "<<i<<" Hand Value: "<<tempVal<<endl;
			if(tempVal>21)
			  cout<<"BUST!"<<endl;
		      }
		      else
			cout<<"Not enough money in bank please choose 1 for HIT or 2 for STAND"<<endl;
		      switchCounter++;
		      break;
		    default : cout<<"Responce not recognized"<<endl;
		      break;
		    }
		}
		else{
		  switch(switchNum)
		    {
		    case 1 : playerAry[i]->DealCard(newDeck);
		      playerAry[i]->PrintHand();
		      tempVal = playerAry[i]->BlackjackValue();
		      cout<<"Player "<<i<<" Hand Value: "<<tempVal<<endl;
		      if(tempVal>21)
			{
			  cout<<"BUST!\n";
			}
		      if(tempVal==21)
			STAND = true;
		      switchCounter++;
		      break;
		      //case 2 stand
		    case 2 : STAND = true; 
		      switchCounter++;
		      break;
		    default : cout<<"Response not recognized"<<endl;
		      break;
		    }
		}
		
		
	      }
	  }
      
    //client's turn fuck ya
    // wait.. fuck no :(

	else if(i == 2)
	  {
	    int switchCounter = 0;
	    char* switchAnswer;
	    while((tempVal<21)&&!STAND)
	      {
		if(switchCounter==0){
		  
		  
		  switchAnswer = gameServer.runServer("1_to_hit_2_to_stand_3_to_double_down_");
		  switchAnswer = gameServer.runServer("_");
		  switchNum = atoi(switchAnswer);
		  
		  
		  
		  
		}
		else{
		  
		  switchAnswer = gameServer.runServer("1_to_hit_2_to_stand_");
		  switchAnswer = gameServer.runServer("_");
		  switchNum = atoi(switchAnswer);
		}
		//cin>>switchNum;
		
		if(switchCounter==0){
		  const char* switchAnswer2;
		  string tempthing;
		  switch(switchNum)
		    {
		    case 1 : playerAry[i]->DealCard(newDeck);
		      playerAry[i]->PrintHandForClient();
		      
		      tempVal = playerAry[i]->BlackjackValue();
		      switchAnswer = gameServer.runServer("Your_Hand_Value_is_");
		      
		      ss << tempVal;
		      tempthing = ss.str();
		      
		      // string tempthing = itoa(tempVal);
		      switchAnswer2 = tempthing.c_str();
		      switchAnswer = gameServer.runServer(switchAnswer2);
		      
		      if(tempVal>21)
			{
			  switchAnswer = gameServer.runServer("BUST");
			}
		      if(tempVal==21) 
			STAND = true;
		      switchCounter++;
		      break;
		      //case 2 stand
		    case 2 : STAND = true; 
		      switchCounter++;
		      break;
		      //double down
		    case 3 : STAND = playerAry[i]->DoubleDown();
		      if(STAND){
			playerAry[i]->DealCard(newDeck);
			playerAry[i]->PrintHandForClient();
			tempVal = playerAry[i]->BlackjackValue();
			switchAnswer = gameServer.runServer("Your_Hand_Value_is_");
			if(tempVal>21)
			  switchAnswer = gameServer.runServer("BUST");
		      }
		      else
			switchAnswer = gameServer.runServer("Not_enough_money_try_again");
		      switchCounter++;
		      break;
		    default : switchAnswer = gameServer.runServer("Responce_not_recognized");
		      break;
		    }
		}
		else{
		  string tempthing;
		  const char* tempthing2;
		  switch(switchNum)
		    {
		   case 1 : playerAry[i]->DealCard(newDeck);
		      playerAry[i]->PrintHandForClient();
		      
		      tempVal = playerAry[i]->BlackjackValue();
		      switchAnswer = gameServer.runServer("Your_Hand_Value_is_");
		      ss << tempVal;
		      tempthing = ss.str();
		      tempthing2 = tempthing.c_str();
		      switchAnswer = gameServer.runServer(tempthing2);
		      
		      if(tempVal>21)
			{
			  switchAnswer = gameServer.runServer("BUST");
			}
		      if(tempVal==21) 
			STAND = true;
		      switchCounter++;
		      break;
		      //case 2 stand
		    case 2 : STAND = true; 
		      switchCounter++;
		      break;
		   default : switchAnswer = gameServer.runServer("Responce_not_recognized");
		      break;
		    }
		}
		
		
	      }
	  }
      }
    cout<<endl;

    int dealerVal = playerAry[0]->BlackjackValue();
    
    cout<<"Dealer: "<<dealerVal<<endl;
    char * whocares = gameServer.runServer("Dealer_");
    ss << dealerVal;    
    string tempString = ss.str();
    const char* dealerValCon = tempString.c_str();
    whocares = gameServer.runServer(dealerValCon);
    

    while(dealerVal<17)
      {
	playerAry[0]->DealCard(newDeck);
	dealerVal = playerAry[0]->BlackjackValue();
	cout<<"Dealer: "<<dealerVal<<endl;
	whocares = gameServer.runServer("Dealer_");
	ss << dealerVal;
	tempString = ss.str();
	dealerValCon = tempString.c_str();
	whocares = gameServer.runServer(dealerValCon);
      }
    dealerVal = playerAry[0]->BlackjackValue();
    if(dealerVal>21){
      cout<<"Dealer BUST!!!"<<endl;
      whocares = gameServer.runServer("Dealer_BUST");
    }    

    for(i=1;i<3;i++)
      {
	tempVal=playerAry[i]->BlackjackValue();
	if(((tempVal>dealerVal)&&(tempVal<22)&&(dealerVal<22))||((tempVal<22)&&(dealerVal>21)))
	  {
	    cout<<"Player "<<i<<" Wins!!!"<<endl;
	    if(i == 1)
	      {
		whocares = gameServer.runServer("Player_1_Wins");
	      }
	    else if(i==2)
	      {
		whocares = gameServer.runServer("Player_2_Wins");
	      }
	    //INSERT CODE TO ADJUST BANK ACOUNT FOR PLAYER
	    playerAry[i]->GetWinnings(2*(playerAry[i]->GetWager()));
	  }
	else if((tempVal==dealerVal)&&(tempVal<22)){
	  cout<<"Player "<<i<<" Pushes."<<endl;
	  if(i == 1)
	      {
		whocares = gameServer.runServer("Player_1_Pushes");
	      }
	  else if(i==2)
	      {
		whocares = gameServer.runServer("Player_2_Pushes");
	      }
	  playerAry[i]->GetWinnings(playerAry[i]->GetWager());
	}
	else
	  {
	    cout<<"Player "<<i<<" Lost."<<endl;
	    if(i == 1)
	      {
		whocares = gameServer.runServer("Player_1_Lost");
	      }
	    else if(i==2)
	      {
	      whocares = gameServer.runServer("Player_2_Lost");
	    }
	    playerAry[i]->LostHand();
	  }
      }
    
    cout<<endl;

    cout<<"Would you like to play another round?"<<endl
	<<"Please enter 1 for yes"<<endl
	<<"or 0 for no. ";
    int playAgain;
    cin>>playAgain;
    if(playAgain!=1){
      PLAY_AGAIN = false;
      return;
    }
  }

}

//MIGHT NOT BE NEEDED...
void Blackjack::Client(){

  // ask for ip
  // ask for name

  // send name

  // ask for bank
  // send bank

  // ask for user to wait
  // ask for user to type ready
  // send ready and receive first step

}

